package com.syqc.maps.clusters

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.syqc.maps.utils.BitmapUtil
import com.syqc.comlib.entity.MapMarker
import com.syqc.comlib.utils.sub
import com.syqc.monitor.R
import com.syqc.monitor.databinding.MarkerViewBinding
import com.syqc.utils.Units

/** @author joker @date 2022/7/22 11:29$ */
class MarkerView(
    context: Context,
    markerSet: MapMarker
) : LinearLayout(context) {
    val binding: MarkerViewBinding =
        MarkerViewBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        initView(markerSet)
    }

    private fun initView(markerSet: MapMarker) {
        if (markerSet.showSpeed) {
            binding.tvMarkerSpeed.isVisible = true
            binding.tvMarkerSpeed.textSize = markerSet.textSize.toFloat()

        } else {
            binding.tvMarkerSpeed.isVisible = false
        }

        val layoutParams = binding.ivMarkerLogo.layoutParams
        layoutParams.width = markerSet.iconSize
        layoutParams.height = markerSet.iconSize
        binding.ivMarkerLogo.layoutParams = layoutParams
        //字体大小
        binding.tvMarkerTitle.textSize = markerSet.textSize.toFloat()
        binding.tvMarkerTitle.setTextColor(Color.parseColor(markerSet.textColor))
        binding.tvMarkerSpeed.setTextColor(Color.parseColor(markerSet.textColor))
        binding.tvMarkerTitle.setBackgroundColor(Color.parseColor(markerSet.backCarColor))
        binding.tvMarkerSpeed.setBackgroundColor(Color.parseColor(markerSet.backSpeedColor))
    }

    /**
     * 设置图标参数
     *
     * @param plate
     * @param iconLink
     * @param speed
     * @param state
     * @param drct
     */
    fun setMarkerInfo(
        plate: String, iconLink: String, state: String, drct: String, speed: String
    ) {
        binding.tvMarkerTitle.text = sub(plate, 8)
        binding.ivMarkerLogo.setImageBitmap(BitmapUtil.getBitmap(context, iconLink))
        binding.tvMarkerSpeed.text = speed + "${Units.speed}"
        if (state.toInt() < 9) {
            binding.ivMarkerLogo.rotation = drct.toFloat()
        } else {
            binding.ivMarkerLogo.rotation = 0f
        }
    }

    /**
     * 设置图标参数
     *
     * @param clusterItem
     * @param isPlate
     */
    fun setMarkerInfo(clusterItem: ClusterItem, isPlate: Boolean) {
        binding.tvMarkerTitle.text = sub(if (isPlate) clusterItem.plate else clusterItem.name, 8)
        binding.ivMarkerLogo.setImageBitmap(BitmapUtil.getBitmap(context, clusterItem.iconLink))
        binding.tvMarkerSpeed.text = clusterItem.speed + "${Units.speed}"
        if (clusterItem.state.toInt() < 9) {
            binding.ivMarkerLogo.rotation = clusterItem.drct.toFloat()
        } else {
            binding.ivMarkerLogo.rotation = 0f
        }
    }

    fun setOnlyMarker(clusterItem: ClusterItem, isPlate: Boolean) {
        setMarkerInfo(clusterItem, isPlate)
        binding.tvMarkerTitle.setTextColor(ContextCompat.getColor(context, R.color.white))
        binding.tvMarkerTitle.setBackgroundResource(R.color.color_FF0)
    }
}